#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "calibrator.h"
#include <QString>
#include <QFileDialog>
#include <QTextCodec>
#include <string>
#include <QThread>
#include <QDebug>

using namespace std;

MainWindow::MainWindow(int argc, char** argv, QWidget *parent)
    : m_argc(argc), m_argv(argv), QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    qDebug() << "窗体线程" << QThread::currentThreadId();

    Calibrator* p_caltor = new Calibrator(m_argc, m_argv);
    QThread* p_calThread = new QThread();
    p_caltor->moveToThread(p_calThread);

    // 标定信号连接
    connect(ui->btn_Cal, &QPushButton::clicked, p_caltor, Calibrator::calMainRun);
    // 校准信号连接
    connect(ui->btn_DoCal, &QPushButton::clicked, this, &MainWindow::selectImg);
    connect(this, &MainWindow::sg_DoCal, p_caltor, &Calibrator::doCal);

    // 窗体关闭后销毁线程和标定对象
    connect(this, &MainWindow::destroyed, p_calThread, &QThread::quit);
    connect(p_calThread, &QThread::finished, p_caltor, Calibrator::deleteLater);
    connect(p_calThread, &QThread::finished, p_calThread, QThread::deleteLater);

    p_calThread->start();

}

void MainWindow::selectImg() {
    QString qstring = QFileDialog::getOpenFileName(this, tr("选择图片"), "E:/Desktop", "*.png *.jpg *.bmp *.tif");
    QTextCodec* code = QTextCodec::codecForName("GB2312");
    string path = code->fromUnicode(qstring).data();
    emit this->sg_DoCal(path);
}

MainWindow::~MainWindow()
{

    delete ui;
}

